<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 服务产品 Model模型类
 */
class ProductModel extends Model{
	
	protected $_validate = array(
		array('img_name','require','产品封面必须填写！'),
		array('title','require', '产品名必须填写！'),
	);
	
	protected $_auto = array(
		array('created','time',1,'function'), //新增时
	);
	
	/**
	 * 添加服务
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function addProduct($data) {
		$this->startTrans();
		$autoModel = D('AutoId');
		$id = $autoModel->createAutoId(array('type' => 'product'));
		 
		$time = time();
		
		$addData ['id'] = $id;
		$addData ['img_name'] = $data ['img_name'];
		$addData ['title'] = $data ['title'];
		$addData ['created'] = $time;
		
		if (!$this->create($addData, 1)) {
			$this->rollback();
			return false;
		} else {
			if (!$this->add()) {
				$this->error = '添加产品失败！';
				$this->rollback();
				return false;
			}
		}
		
		$contents = I('post.content');
		 
		if ($contents) {
			$ContentModel = D('Content');
			foreach ($contents as $key => $content) {
				$contentData = array(
					'target_id' => $id,
					'title' => '',
					'content' => $content,
					'created' => $time
				);
				$stepAddResult = $ContentModel->add($contentData);
				if (!$stepAddResult) {
					$this->rollback();
					$this->error = '添加步骤失败';
					return false;
				}
			}
		}
		
		$this->commit();
		return true;
	}
	
	public function clearCache($id) {
		S('Product_Item_'.$id, null);
	}
	
	public function getCacheKey($id) {
		return 'Product_Item_'.$id;
	}
	
	/**
	 * 读取服务信息
	 * 
	 * @param number $id	产品编号
	 * @return unknown
	 */
	public function getProductInfo($id) {
		$cacheKey = $this->getCacheKey($id);
		$info = S($cacheKey);
		if (!$info) {
			$info = $this->where(array('id' => $id))->find();
			S($cacheKey, $info);
		}
		
		return $info;
	}
	
	/**
	 * 保存编辑
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function updateInfo($data) {
		$this->startTrans();
		$id = $data ['id'];
		
		$updateData ['img_name'] = $data ['img_name'];
		$updateData ['title'] = $data ['title'];
		
		$item = $this->getProductInfo($id);
		
		unset($item ['id']);
		unset($item ['created']);
		unset($item ['status']);
		
		$hasModify = array_diff($updateData, $item);
		if ($hasModify && !$this->where(array('id' => $id))->save($updateData)) {
			$this->error = '编辑产品失败！';
			$this->rollback();
			return false;
		}
		
		$ContentModel = D('Content');
		$ids = $data ['step_id'];
		$editResult = $ContentModel->editContent($id, $ids, $data ['content'], $data ['title']);
		if (!$editResult) {
			$this->error = '编辑产品功效失败！';
			$this->rollback();
			return false;
		}
		
		$cacheKey = $this->getCacheKey($id);
		S($cacheKey, NULL);
		$this->commit();
		return true;
	}
}